package com.qf.eduadmin.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.qf.eduadmin.mapper.TbPermissionDao;
import com.qf.eduadmin.pojo.req.TbPermissionReq;
import com.qf.eduadmin.pojo.resp.ResponseData;
import com.qf.eduadmin.pojo.vo.TbRolePermission;
import com.qf.eduadmin.pojo.resp.TreeResp;
import com.qf.eduadmin.pojo.vo.TbPermission;
import com.qf.eduadmin.service.TbPermissionService;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

/**
 * (TbPermission)表服务实现类
 *
 * @author makejava
 * @since 2021-12-27 21:01:29
 */
@Service("tbPermissionService")
public class TbPermissionServiceImpl implements TbPermissionService {
    @Resource
    private TbPermissionDao tbPermissionDao;

    /**
     * 通过ID查询单条数据
     *
     * @param permissionid 主键
     * @return 实例对象
     */
    @Override
    public ResponseData queryById(Integer permissionid) {
        TbPermission tbPermission = tbPermissionDao.queryById(permissionid);
        if(tbPermission!=null){
            return new ResponseData(0,"查询成功",tbPermission,null);
        }
        return null;
    }

    /**
     * 查询多条数据
     *
     * @param page 查询起始位置
     * @param limit 查询条数
     * @return 对象列表
     */
    @Override
    public ResponseData queryAllByLimit(Integer page, Integer limit) {
        PageHelper.startPage(page,limit);
        List<TbPermission> tbPermissions = tbPermissionDao.queryAllByLimit();
        PageInfo<TbPermission> of = PageInfo.of(tbPermissions);
        if (tbPermissions.size()>0){
            return new ResponseData(0,"查询成功",of.getList(),of.getTotal());
        }
        return null;
    }

    /**
     * 新增数据
     *
     * @param tbPermissionReq 实例对象
     * @return 实例对象
     */
    @Override
    public ResponseData insert(TbPermissionReq tbPermissionReq) {
        TbPermission tbPermission = new TbPermission();
        BeanUtils.copyProperties(tbPermissionReq,tbPermission);
        Integer insert = tbPermissionDao.insert(tbPermission);
        if(insert!=null){
            return new ResponseData(0,"添加成功",null,null);
        }
        return null;
    }

    /**
     * 修改数据
     *
     * @param tbPermission 实例对象
     * @return 实例对象
     */
    @Override
    public ResponseData update(TbPermission tbPermission) {
        Integer integer = tbPermissionDao.update(tbPermission);
        if(integer!=null){
            return new ResponseData(0,"更新成功",null,null);
        }
        return null;
    }

    /**
     * 通过主键删除数据
     *
     * @param permissionid 主键
     * @return 是否成功
     */
    @Override
    public ResponseData deleteById(Integer permissionid) {
        Integer integer = tbPermissionDao.deleteById(permissionid);
        if(integer!=null){
            return new ResponseData(0,"删除成功",null,null);
        }
        return null;
    }

    @Override
    public ResponseData findPermissionByRoleId(Integer roleId) {
        //查询出该角色拥有的权限
        List<TbRolePermission> perList = tbPermissionDao.findPermissionByRoleId(roleId);
        //System.out.println(perList);
        List pers = new ArrayList();
        for(TbRolePermission tbRolePermission : perList){
            //循环通过角色id查询出的权限，如果有角色Id 则代表该权限已经被角色绑定，如果没有则代表未被绑定
            //声明返回对象
            TreeResp treeResp = new TreeResp();
            if(tbRolePermission.getRoleid()!=null){
                treeResp.setChecked(true);
            }
            treeResp.setId(tbRolePermission.getPermissionid());
            treeResp.setTitle(tbRolePermission.getPermissionName());
            pers.add(treeResp);
        }
        if(perList.size()>0){
            return new ResponseData(0,"查询成功",pers,null);
        }
        return new ResponseData(1,"查询失败",null,null);
    }
}